C/C++

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C/C++ >

C++_C++求1到n中1出现的次数以及数的二进制表示中1的个数,在从 1 到 n 的正数中 1 出现的

来源:网络整理  作者:  发布时间:2020-12-19 05:40
C++求1到n中1出现的次数以及数的二进制表示中1的个数,在从 1 到 n 的正数中 1 出现的次数题目:输入一个整数 n,求从...

解法三比较奇妙,每次将数字的最后一位处理成0,统计处理的次数,进而统计1的个数

总结其中每位上1出现的规律即可得到方法二。其时间复杂度为O(Len),Len为数字长度

输入一个整数,求该整数的二进制表达中有多少个 1。

方法一就是从1开始遍历到N,将其中的每一个数中含有“1”的个数加起来,比较好想。

整数的二进制表示中 1 的个数

百位出现1的数字:100~123

解法一是普通处理方式,通过除二余二统计1的个数;

要求:

代码实现(GCC编译通过):

解法二与解法一类似,通过向右位移依次处理,每次与1按位与统计1的个数

#include "stdio.h" #include "stdlib.h" int count1(int n); int count2(int n); int main(void) { int x; printf("输入一个数:"); scanf("%d",&x); printf("\n从0到%d一共遇到%d(%d)个1\n",x,count1(x),count2(x)); return 0; } //解法一 int count1(int n) { int count = 0; int i,t; //遍历1到n for(i=1;i<=n;i++) { t=i; //依次处理当前遍历到的数字的各个位 while(t != 0) { //若为1则统计加一 count += (t%10 == 1)?1:0; t/=10; } } return count; } //解法二: int count2(int n) { int count = 0;//统计变量 int factor = 1;//分解因子 int lower = 0;//当前处理位的所有低位 int higher = 0;//当前处理位的所有高位 int curr =0;//当前处理位 while(n/factor != 0) { lower = n - n/factor*factor;//求得低位 curr = (n/factor)%10;//求当前位 higher = n/(factor*10);//求高位 switch(curr) { case 0: count += higher * factor; break; case 1: count += higher * factor + lower + 1; break; default: count += (higher+1)*factor; } factor *= 10; } return count; }

方法二比较有意思,核心思路是这样的:统计每一位上可能出现1的次数。

例如输入 10,由于其二进制表示为 1010,有两个 1,因此输出 2。

代码实现(GCC编译通过):

分析:

个位出现1的数字:1,11,13,21,31,...,91,101,111,121

#include "stdio.h" #include "stdlib.h" int count1(int x); int count2(int x); int count3(int x); int main(void) { int x; printf("输入一个数:\n"); setbuf(stdin,NULL); scanf("%d",&x); printf("%d转二进制中1的个数是:",x); printf("\n解法一:%d",count1(x)); printf("\n解法二:%d",count2(x)); printf("\n解法三:%d",count3(x)); printf("\n"); return 0; } //除二、余二依次统计每位 int count1(int x) { int c=0; while(x) { if(x%2==1) c++; x/=2; } return c; } //向右移位,与1按位与统计每位 int count2(int x) { int c=0; while(x) { c+=x & 0x1; x>>=1; } return c; } //每次将最后一个1处理成0,统计处理次数 int count3(int x) { int c=0; while(x) { x&=(x-1); c++; } return c; }

题目:整数的二进制表示中 1 的个数

例如输入 12,从 1 到 12 这些整数中包含 1  的数字有 1, 10, 1 1 和 12, 1 一共出现了 5 次


比如123:

十位出现1的数字:10~19,110~119

分析:

相关热词: C++

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/cjj/5484.shtml

最新文章
只需要在调用Ctrl+B编译后 只需要在调用Ctrl+B编译后

时间:2021-01-13

OpenGL超级宝典visual studio OpenGL超级宝典visual studio

时间:2021-01-04

Directx11 教程(2) 基本的wi Directx11 教程(2) 基本的wi

时间:2021-01-04

LeetCode11ContainerWithMostWate LeetCode11ContainerWithMostWate

时间:2021-01-04

C语言简单IT之家速成 C语言简单IT之家速成

时间:2020-12-27

三分钟了解Activity工作流 三分钟了解Activity工作流

时间:2020-12-27

编译器是如何实现32位整型 编译器是如何实现32位整型

时间:2020-12-27

C++中lower_bound函数和upper C++中lower_bound函数和upper

时间:2020-12-27

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C++_C++求1到n中1出现的次数以及数的二进制表示中1的个数,在从 1 到 n 的正数中 1 出现的

2020-12-19 编辑:

解法三比较奇妙,每次将数字的最后一位处理成0,统计处理的次数,进而统计1的个数

总结其中每位上1出现的规律即可得到方法二。其时间复杂度为O(Len),Len为数字长度

输入一个整数,求该整数的二进制表达中有多少个 1。

方法一就是从1开始遍历到N,将其中的每一个数中含有“1”的个数加起来,比较好想。

整数的二进制表示中 1 的个数

百位出现1的数字:100~123

解法一是普通处理方式,通过除二余二统计1的个数;

要求:

代码实现(GCC编译通过):

解法二与解法一类似,通过向右位移依次处理,每次与1按位与统计1的个数

#include "stdio.h" #include "stdlib.h" int count1(int n); int count2(int n); int main(void) { int x; printf("输入一个数:"); scanf("%d",&x); printf("\n从0到%d一共遇到%d(%d)个1\n",x,count1(x),count2(x)); return 0; } //解法一 int count1(int n) { int count = 0; int i,t; //遍历1到n for(i=1;i<=n;i++) { t=i; //依次处理当前遍历到的数字的各个位 while(t != 0) { //若为1则统计加一 count += (t%10 == 1)?1:0; t/=10; } } return count; } //解法二: int count2(int n) { int count = 0;//统计变量 int factor = 1;//分解因子 int lower = 0;//当前处理位的所有低位 int higher = 0;//当前处理位的所有高位 int curr =0;//当前处理位 while(n/factor != 0) { lower = n - n/factor*factor;//求得低位 curr = (n/factor)%10;//求当前位 higher = n/(factor*10);//求高位 switch(curr) { case 0: count += higher * factor; break; case 1: count += higher * factor + lower + 1; break; default: count += (higher+1)*factor; } factor *= 10; } return count; }

方法二比较有意思,核心思路是这样的:统计每一位上可能出现1的次数。

例如输入 10,由于其二进制表示为 1010,有两个 1,因此输出 2。

代码实现(GCC编译通过):

分析:

个位出现1的数字:1,11,13,21,31,...,91,101,111,121

#include "stdio.h" #include "stdlib.h" int count1(int x); int count2(int x); int count3(int x); int main(void) { int x; printf("输入一个数:\n"); setbuf(stdin,NULL); scanf("%d",&x); printf("%d转二进制中1的个数是:",x); printf("\n解法一:%d",count1(x)); printf("\n解法二:%d",count2(x)); printf("\n解法三:%d",count3(x)); printf("\n"); return 0; } //除二、余二依次统计每位 int count1(int x) { int c=0; while(x) { if(x%2==1) c++; x/=2; } return c; } //向右移位,与1按位与统计每位 int count2(int x) { int c=0; while(x) { c+=x & 0x1; x>>=1; } return c; } //每次将最后一个1处理成0,统计处理次数 int count3(int x) { int c=0; while(x) { x&=(x-1); c++; } return c; }

题目:整数的二进制表示中 1 的个数

例如输入 12,从 1 到 12 这些整数中包含 1  的数字有 1, 10, 1 1 和 12, 1 一共出现了 5 次


比如123:

十位出现1的数字:10~19,110~119

分析:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/cjj/5484.shtml

相关文章

风云图片

推荐阅读

返回C/C++频道首页